// pages/coach-info/coach-info.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    date: [],
    id:'',
    heading:'',
    name:'',
    skill:'',
    count:'',
    price:'',
    certificate:[],
    personal:[],
    introduce:'',
    order:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDate(),
    this.setDate(),
    this.getweek(),
    this.getMessage(options.id)

  },
  getMessage(coachId){
    request({
      url: '/coach/info/'+coachId,
      method: 'GET',
    }).then(res =>{
      console.log(res);
      if(res.code === 200){
        const { img,coachName,skill,count,price,cert,personal,introduce,id} = res.data
        this.setData({
          id,
          img,
          coachName,
          skill,
          count,
          price,
          cert,
          personal,
          introduce
        })
      }else{
        wx.showToast({
          icon: 'error',
          title: res.message,
        })
      }
    }).catch(err => {
      console.error(err);
    });
  },
  changeAct(e){
    this.setData({
      active:e.target.dataset.id
    })
  },
  getDate(AddDayCount){
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return m+"-"+d;
  },
  getweek(i){
    let today = new Date();
    let weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    let nextDay = new Date(today.getTime() + 24 * 60 * 60 * 1000 * i);
    return weekdays[nextDay.getDay()]
  },
  setDate(){
    this.setData({
      date:[{week:'今天',Date:this.getDate(0)},
            {week:this.getweek(1),Date:this.getDate(1)},
            {week:this.getweek(2),Date:this.getDate(2)},
            {week:this.getweek(3),Date:this.getDate(3)},
            {week:this.getweek(4),Date:this.getDate(4)},
            {week:this.getweek(5),Date:this.getDate(5)},
            {week:this.getweek(6),Date:this.getDate(6)}]
    })
  },
 toOrder(){
   let ordersCourseDTO = {
    coachId:this.data.id,
    payment:this.data.price
   }
   request({
     url:'/order/coach',
     method:'post',
     data:{
      ordersCourseDTO
     }
   }).then(res=>{
     console.log(res);
     this.setData({
       order:false
     })
     wx.showToast({
      icon: 'success',
      title: '预约成功',
    })
   }).catch(req=>{
    console.log(req);
  })
 },
 noOrder(){
   let id = this.data.id
   request({
     url:'/order/appointCoach/'+id,
     method:'delete'
   }).then(res=>{
     console.log(res);
     this.setData({
       order:true
     })
     wx.showToast({
      icon: 'success',
      title: '取消预约成功',
    })
   }).catch(req=>{
     console.log(req);
   })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})